//
//  xsBossIPBottomTitleToolBarView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/29.
// @[@"老板IP",@"配音",@"背景",@"文字",@"音乐"]

#import "xsBossIPBottomTitleToolBarView.h"
#import "NSString+WordSize.h"
#define xrandomColor  [UIColor colorWithRed:(arc4random() %255)/255.0f green:(arc4random() %255)/255.0f blue:(arc4random() %255)/255.0f alpha:1.0f]

@interface xsBossIPBottomTitleToolBarView()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *bgScrollView;
@property (nonatomic, strong) NSArray *buttonTitle;
@property (nonatomic, strong) NSArray *buttonIcons;
@property (nonatomic, strong) UIView *xsContentView;

@end

@implementation xsBossIPBottomTitleToolBarView


- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles withIcons:(NSArray *)buttonIconArr{
    
    if (self = [super init]) {
        self.backgroundColor = UIColor.clearColor;
        
        self.buttonTitle = buttonTitles;
        self.buttonIcons = buttonIconArr;
 
        [self addSubview:self.bgScrollView];

        [self.bgScrollView addSubview:self.xsContentView];
            

    }
    return self;
}


- (UIView *)xsContentView{
    if(!_xsContentView){
        _xsContentView = [[UIView alloc] init];
        _xsContentView.backgroundColor = UIColor.clearColor;
    }
    return _xsContentView;
}


- (UIScrollView *)bgScrollView
{
    if(!_bgScrollView){
        _bgScrollView = [[UIScrollView alloc] init];
        _bgScrollView.backgroundColor = UIColor.clearColor;
        
        _bgScrollView.delegate = self;
        _bgScrollView.pagingEnabled = YES;
        _bgScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _bgScrollView;
}


- (void)buttonClicked:(UIButton *)sender {
    NSInteger selectedIndex = sender.tag;
    NSLog(@"Selected index: %ld", (long)selectedIndex);
    
    if([self.delegate respondsToSelector:@selector(xsDigitalRobotButtonBeClick:)]){
        [self.delegate xsDigitalRobotButtonBeClick:sender];
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
    //_recorder.preViewLayer.frame = self.frame;
    
    NSLog(@"layoutSubviews--self.bounds==%@",NSStringFromCGRect(self.bounds));
    
    self.bgScrollView.frame = CGRectMake(10, 0, self.bounds.size.width-20, 50);
    
    UIFont *btnFont = [UIFont systemFontOfSize:16];
    
    CGFloat buttonH = 50.0;
    CGFloat buttonW = self.bgScrollView.frame.size.width*0.2;
    UIButton *lastButton;
    // 添加按钮到滚动视图
    for (int i = 0; i < self.buttonTitle.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *buttonTitleStr = self.buttonTitle[i];
        
//        CGFloat buttonW = buttonH;//[buttonTitleStr calculateWidth:btnFont withHeight:buttonH];
        
        button.frame = CGRectMake(i * buttonW, 0, buttonW, buttonH);
//        if(lastButton){
//            button.frame = CGRectMake(i * buttonW, 0, buttonW, 50);
//        }
//        else{
//            button.frame = CGRectMake(i * buttonW, 0, buttonW, 50);
//        }
        button.backgroundColor =  UIColor.clearColor;//xrandomColor;//xrandomColor;
//        [button setTitle:self.buttonTitle[i] forState:UIControlStateNormal];
//        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        //button.font = btnFont;
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [self.xsContentView addSubview:button];
        
        
        CGFloat buttonImageViewWH = buttonH *0.5;
        CGFloat buttonImageViewX = (buttonW-buttonImageViewWH)*0.5;
        CGFloat buttonImageViewY = 3.0;
        
        NSString *buttonImageViewStr = self.buttonIcons[i];
        UIImage *inggg =  [UIImage imageNamed:buttonImageViewStr];
        UIImageView *buttonImageView = [[UIImageView alloc] init];
        buttonImageView.backgroundColor  =  UIColor.clearColor;//xrandomColor;//
        buttonImageView.userInteractionEnabled = NO;
        [button addSubview:buttonImageView];
        buttonImageView.frame = CGRectMake(buttonImageViewX, buttonImageViewY, buttonImageViewWH, buttonImageViewWH);
        buttonImageView.image = inggg;
        
        CGFloat buttonTitleLabelW = buttonW;
        CGFloat buttonTitleLabelX = 0;
        CGFloat buttonTitleLabelY = CGRectGetMaxY(buttonImageView.frame);
        CGFloat buttonTitleLabelH = buttonH-buttonTitleLabelY-3;
        UILabel *buttonTitleLabel = [[UILabel alloc] init];
        buttonTitleLabel.backgroundColor = UIColor.clearColor;//xrandomColor;//
        buttonTitleLabel.font = [UIFont systemFontOfSize:15];
        buttonTitleLabel.textColor = UIColor.whiteColor;
        buttonTitleLabel.numberOfLines = 1;
        buttonTitleLabel.text = buttonTitleStr;
        buttonTitleLabel.textAlignment = NSTextAlignmentCenter;
        [button addSubview:buttonTitleLabel];
        buttonTitleLabel.frame = CGRectMake(buttonTitleLabelX, buttonTitleLabelY, buttonTitleLabelW, buttonTitleLabelH);
        
        
        lastButton = button;
    }

    // 设置内容视图的尺寸
    self.xsContentView.frame = CGRectMake(0, 0, CGRectGetMaxX(lastButton.frame), 50);
    self.bgScrollView.contentSize = self.xsContentView.frame.size;
}




- (void)updateConstraints{
    [super updateConstraints];
    
    __weak __typeof (&*self)weakSelf = self;
    
    NSLog(@"updateConstraints");
    
    
    
    
}



@end
